package com.jxwifi.cloud.quickclean.order;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.idroid.utils.ListDataSave;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.RemarkBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class AddRemarksPresenter {

    private static String QR_CODE_BEAN = "QR_REMARKS";
    ListDataSave dataSave;
    private Context mContext;
    private RemarksView mView;
    private AuzImpl mAuz;
    private List<RemarkBean> mListBean;

    public AddRemarksPresenter(Context context, RemarksView view) {
        this.mContext = context;
        this.mView = view;
        mAuz = new AuzImpl();
    }


    public void initView() {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(QR_CODE_BEAN,
                Activity.MODE_PRIVATE);
        String strJson = sharedPreferences.getString(QR_CODE_BEAN, null);
        mListBean = JSON.parseArray(strJson, RemarkBean.class);
        if (mListBean != null) {
            final List<String> mStr = new ArrayList<>();
            for (int i = 0; i < mListBean.size(); i++) {
                mStr.add(mListBean.get(i).getName());
            }
            mView.getTagFlow().setAdapter(new TagAdapter<String>(mStr) {
                @Override
                public View getView(FlowLayout parent, int position, String o) {
                    TextView tv = (TextView) mView.getInflater().inflate(R.layout.tv_label,
                            mView.getTagFlow(), false);
                    tv.setText(o);
                    return tv;
                }
            });
            mView.getTagFlow().setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    view.setBackgroundResource(R.drawable.shap_client_tag_bg);
                    mView.getEvaluation().setText(mStr.get(position));
                    return true;
                }
            });
            mView.getTagFlow().setOnSelectListener(new TagFlowLayout.OnSelectListener() {
                @Override
                public void onSelected(Set<Integer> selectPosSet) {

                }
            });
        }
    }

    public void onRemarks(){
        dataSave = new ListDataSave(mContext, QR_CODE_BEAN);
        RemarkBean mBean = new RemarkBean();
        mBean.setName(mView.getEvaluation().getText().toString());
        if (mListBean!=null){
            boolean mIsidentical=false;
            for (int i=0;i<mListBean.size();i++){
                if (mListBean.get(i).getName().equals(mBean.getName())){
                    mIsidentical=true;
                    break;
                }
            }
            if (!mIsidentical){
                if (mListBean.size()==5){
                    mListBean.remove(0);
                }
                mListBean.add(mBean);
            }
        }else{
            mListBean=new ArrayList<>();
            mListBean.add(mBean);
        }
        dataSave.setDataList(QR_CODE_BEAN, mListBean);
        EventBus.getDefault().post(mBean.getName(), "e-addremarks");
    }

    /**
     * 保存人生信条
     */
    public void saveCreed(){
        Params params=new Params();
        params.add("autograph",mView.getEvaluation().getText().toString());
        mAuz.auzget(UrlPool.POST_UPDATE_PROFILE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code==0){
                    mView.onSuccess(data);
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

}
